<?php
/**
* @package module to redirect editting of content in com_content form the frontend to Pages-and-Items.
* @version 1.0.2
* @copyright Copyright (C) 2006-2008 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

//get vars
if( defined('_JEXEC') ){
	//joomla 1.5
	$framework = '1.5.x';
	$option = JRequest::getVar('option', '', 'get');		
	$task = JRequest::getVar('task', '', 'get');
	$layout = JRequest::getVar('layout', '', 'get');	
	$view = JRequest::getVar('view', '', 'get');
	$path_to_root = '/../../';			
}else{
	//joomla 1.0.x
	$framework = '1.0.x';
	$option = mosGetParam( $_GET, 'option', '' );
	$task = mosGetParam( $_GET, 'task', '' );	
	$view = mosGetParam( $_GET, 'view', '' );
	$path_to_root = '/../';					
}

//only if something is being editted
if(($framework=='1.5.x' && 
($option=='com_content' && ($task=='edit' || $layout=='form')) 
) ||
$framework=='1.0.x' && 	
($option=='com_content' && ($task=='new' || $task=='edit'))
){	

	//get config to check if we need to do a redirect	
	$use_pi_frontend_editting = 0;
	global $database;
	//get database
	if( defined('_JEXEC') ){
		//joomla 1.5
		$database = JFactory::getDBO();
	}
	$database->setQuery("SELECT config "
	."FROM #__pi_config "
	."WHERE id='pi' "
	."LIMIT 1"
	);
	$temp = $database->loadObjectList();
	$temp = $temp[0];
	$raw = $temp->config;
	
	if(strpos($raw,'use_pi_frontend_editting=true')){
		$use_pi_frontend_editting = 1;
	}
		
			
	
	//only redirect if in pi-config use_pi_frontend_editting is activated
	if($use_pi_frontend_editting){
	
		if(defined('_JEXEC')){
			//joomla 1.5							
			$id = JRequest::getVar('id', '', 'get');
			if(strpos($id, ':')){
				$pos = strpos($id, ':');
				$item_id = intval(substr($id, 0, $pos));
			}else{
				$item_id = intval($id);
			}
			$menu_id = JRequest::getVar('Itemid', '', 'get');	
		}else{
			//joomla 1.0.x							
			$item_id = mosGetParam( $_GET, 'id', '');
			$menu_id = mosGetParam( $_REQUEST, 'Itemid', '' );								
		}
	
		//redirect item new						
		if(($framework=='1.5.x' && $option=='com_content' && $view=='article' && $layout=='form') ||
		($framework=='1.0.x' && $option=='com_content' && $task=='new')
		){			
			//echo 'new item';
			$sub_task = 'new';	
		}
		
		//redirect item edit 				
		if(($framework=='1.5.x' && $option=='com_content' && $view=='article' && $task=='edit') ||
		($framework=='1.0.x' && $option=='com_content' && $task=='edit')
		){
			//echo 'item edit';
			$sub_task = 'edit';	
		}	
		
		$url = 'index.php?option=com_pi_pages_and_items&task=item&sub_task='.$sub_task.'&item_id='.$item_id.'&pageId='.$menu_id;
		if(defined('_JEXEC')){
			//joomla 1.5
			global $mainframe;
			$mainframe->redirect($url);
		}else{
			//joomla 1.0.x
			mosRedirect($url);
		}				
	}			
}

?>